package cn.com.ourclass.webviewnew.wb;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.hui.baseutils.R;
import com.hc.framework.mode.BaseActivity;
import com.hc.framework.titlebar.CommonTitleBar;


/**
 * 通用的web页面
 * Created by Admin on 2017/1/20.
 */

public class WebActivity extends BaseActivity {

    private String title,url;
    private WebView webView;
    private ImageView mNonet;
    private LinearLayout back;
    @Override
    protected void initTitle(CommonTitleBar titleBar) {
        TextView title = (TextView)findViewById(R.id.title);
        title.setText("");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        webView = (WebView) findViewById(R.id.common_webview);
        mNonet = (ImageView) findViewById(R.id.nonetwork);
        back = (LinearLayout)findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        isnetwork();
    }
    //监控网络
    public void isnetwork() {
        ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
            //Log.e("ttt", "没网络" );
            mNonet.setVisibility(View.VISIBLE);

            // unconnect network
        }else {
            //webView.setScrollContainer(false); //去掉滚动
            // 设置WebView的客户端
            webView.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;// 返回false
                }
            });
            //设置编码
            webView.getSettings().setDefaultTextEncodingName("utf-8");
            //支持js
            webView.getSettings().setJavaScriptEnabled(true);
            //设置背景颜色 透明
            //webView.setBackgroundColor(Color.argb(0, 0, 0, 0));
            //设置本地调用对象及其接口
            //webView.addJavascriptInterface(new JavaScriptObject(mContext), "myObj");
            //载入js
            webView.loadUrl(url);

            webView.setWebViewClient(new WebViewClient()
            {
                @Override
                public void onPageFinished(WebView view, String url)
                {
                    //开始
                    super.onPageFinished(view, url);
                }
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon)
                {
                    //结束
                    super.onPageStarted(view, url, favicon);

                }
            });

        }
    }

    public static void loadUrl(Context mContext, String mUrl, String title) {
        Intent intent = new Intent(mContext, WebActivity.class);
        intent.putExtra("title","");
        intent.putExtra("url",mUrl);
        mContext.startActivity(intent);
    }


    @Override
    protected void setRootView() {
        setContentView(R.layout.activity_web);
        title = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
    }

}
